#!/usr/bin/env perl

# Rewrite the faces parts of a .gts file to a .mesh file suitable for input to ./mesh2nodal (from the metis package)

# For the wrekagrid project
# Basile Clout, October 2007

use strict;

my $cnt=0;
my ($nv, $ne, $nf);
my @edges;
my ($v1, $v2);
my ($e1, $e2, $e3);
my @vert;

while (<STDIN>) {
  
  @vert = ();
  chomp;
  if ($cnt==0) {
    ($nv, $ne, $nf) = split / /, $_;
    print "$nf 1\n";
  }else {
    
    # Save all the edges
    if ($cnt>$nv && $cnt<$ne+$nv+1) {
      ($v1, $v2) = split / /, $_;
      push @edges, [ $v1, $v2 ];
#      print "edge: $v1, $v2\n";
    }

    # Handle the faces. 
    # The problem is, the elements are made of edges, not nodes!
    # We need to fix that
    if ($cnt > $ne+$nv) {
      ($e1, $e2, $e3) = split / /, $_; 
#      print "$e1, $e2, $e3\n";
      foreach my $e (($e1, $e2, $e3)) { # For each edge of the face, we add one end vertice if it is not already is the list
	$v1 = $edges[$e-1][0];
	$v2 = $edges[$e-1][1];
#	print "v1=$v1, v2=$v2\n";
	push @vert, $v1 if !grep(/^$v1$/, @vert);
	push @vert, $v2 if !grep(/^$v2$/, @vert);
      }
      
      print join(' ', @vert)."\n";
    }
  }
  
  $cnt++;
}

#print "edges: \n";
#foreach my $edge (@edges){
#  print "v1=$edge->[0], v2=$edge->[1]\n";
#}


print "stats: $nv, $ne, $nf\n";
